<template>
  <div class="searchBox">
    <el-card class="box-card">
      <div class="top">
        <div class="top_left">
          <el-icon><Search /></el-icon>
          <span class="search_txt">筛选搜索 </span>
        </div>
        <div class="top_right">
          <el-button type="default" @click="reset">重置</el-button>
          <el-button type="primary" @click="searchParamsFn">查询搜索</el-button>
        </div>
      </div>
      <div class="under">
        <span>优惠券名称： </span>
        <el-input size="small" placeholder="活动名称" v-model="form.name" />
        <div style="margin-left: 10px">
          <span>优惠券类型： </span>
          <el-select v-model="form.type" class="m-2" placeholder="全部">
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
    </el-card>
  </div>
  <div class="add">
    <el-card>
      <el-icon><Tickets /></el-icon>数据列表
      <div class="btn">
        <el-button @click="router.push('/sms/addCoupon')">添加</el-button>
      </div>
    </el-card>
  </div>
  <el-table
    :data="list"
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column label="编号" prop="id" />
    <el-table-column label="优惠券名称" prop="name" />
    <el-table-column label="优惠券类型" prop="type">
      <template #default="{ row }">
        <span>{{
          row.type === 0
            ? "全场赠券"
            : row.type === 1
            ? "会员赠券"
            : row.type === 2
            ? "购物赠券"
            : "注册赠券"
        }}</span>
      </template>
    </el-table-column>
    <el-table-column label="可使用商品" prop="startDate">
      <template #default="{ row }">
        <span>{{ row.useType === 1 ? "指定分类" : "全场通用" }}</span>
      </template>
    </el-table-column>
    <el-table-column label="使用门槛" prop="minPoint">
      <template #default="{ row }">
        <span>满{{ row.minPoint }}元可用</span>
      </template>
    </el-table-column>
    <el-table-column label="面值" prop="amount">
      <template #default="{ row }">
        <span>{{ row.amount }}元</span>
      </template>
    </el-table-column>
    <el-table-column label="适用平台" prop="platform">
      <template #default="{ row }">
        <span>{{
          row.platform === 0
            ? "全平台"
            : row.platform === 1
            ? "移动平台"
            : "PC平台"
        }}</span>
      </template></el-table-column
    >
    <el-table-column label="有效期">
      <template #default="{ row }">
        <span
          >{{ dateFormat(row.startTime) }}至{{ dateFormat(row.endTime) }}</span
        ></template
      >
    </el-table-column>
    <el-table-column label="状态">
      <template #default="{ row }">
        <span>{{
          Date.parse(row.endTime) - new Date().getTime() >= 0
            ? "未过期"
            : "已过期"
        }}</span></template
      >
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'selectSession'"
          size="small"
          @click="router.push(`/sms/couponHistory?id=${row.id}`)"
          >查看</el-button
        >
        <el-button
          text
          type="primary"
          size="small"
          @click="router.push(`/sms/updateCoupon?id=${row.id}`)"
          >编辑</el-button
        >
        <el-button text type="primary" size="small" @click="del(row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="page">
    <el-config-provider :locale="locale">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle"
    /></el-config-provider>
  </div>
</template>
<script setup lang="ts">
import useTable from "@/hooks/useTable";
import { reactive, ref } from "vue";
import { delCouponApi, getCouponListApi } from "./api";
import { Search, Tickets } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { dateFormat } from "@/utils/index";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";

const locale = zhCn;
const router = useRouter();
const form = reactive({
  name: "",
  type: "",
});
const option = [
  {
    value: "0",
    label: "全场赠券",
  },
  {
    value: "1",
    label: "会员赠券",
  },
  {
    value: "2",
    label: "购物赠券",
  },
  {
    value: "3",
    label: "注册赠券",
  },
];
// 重置
const reset = () => {
  form.name = "";
  form.type = "";
  searchParamsFn();
};
//删除
const del = (id: number) => {
  ElMessageBox.confirm("确定删除吗？", "删除优惠券", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(() => {
    delCouponApi(id)
      .then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          searchParamsFn();
        }
      })
      .catch(() => {});
  });
};
const {
  list,
  pageNum,
  total,
  pageChangeHandle,
  pageSizeChangeHandle,
  pageSize,
  searchParamsFn,
} = useTable(getCouponListApi, form);
</script>
<style lang="less" scoped>
.searchBox {
  :deep(.el-card) {
    padding: 20px;
  }
  .top {
    position: relative;
    display: flex;
    .top_left {
      .search_txt {
        padding-left: 10px;
        font-size: 14px;
      }
    }
    .top_right {
      position: absolute;
      right: 20px;
    }
  }
  .under {
    display: flex;
    align-items: center;
    padding: 20px 40px 0;
    font-size: 14px;
    :deep(.el-input) {
      height: 32px;
      line-height: 32px;
      width: 210px;
    }
  }
}
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    position: relative;
    // flex-wrap: wrap;
    // justify-content: space-between;
    align-items: center;
  }
  .btn {
    position: absolute;
    right: 10px;
  }
}
:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 12px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
}
</style>
